<?php

require_once 'Ext/User/Edit/Email/Interface.php';

class Ext_User_Edit_Email_Adapter_Ucenter extends Ext_User_Edit_Email_Abstract
{
    
    const NOTHING                 = 'nothing';
    const RECORD_NOT_FOUND        = 'RecordNotFound';
    const EMAIL_FORMAT_ERROR      = 'emailFormatError';
    const EMAIL_IS_DENIED         = 'emailIsDenied';
    const EMAIL_ALREADY_EXISTS    = 'emailAlreadyExists';
    const USER_IS_PROTECT         = 'userIsProtect';
    const UNCATEGORIZED           = 'uncategorized';
    protected $_messageTemplates = array(
        self::NOTHING                => "Nothing.",
        self::RECORD_NOT_FOUND       => "Record not found.",
        self::EMAIL_FORMAT_ERROR     => "Email format error.",
        self::EMAIL_IS_DENIED        => "Email is denied.",
        self::EMAIL_ALREADY_EXISTS   => "Email '%email%' already exists.",
        self::USER_IS_PROTECT        => "The user is protect.",
        self::UNCATEGORIZED          => "Uncategorized.",
    );
    
    public function edit($email)
    {
        $result = array(
            'code'        => Ext_User_Edit_Email_Result::FAILURE,
            'messages'    => array(),
        );
            
        $userProfile = App_Model_DbTable_User_Profile::getInstance()->getRowByUserId($this->_userId);
        if(!$userProfile){
            $result['messages'][] = $this->_messager->createMessage(self::RECORD_NOT_FOUND);
            return new Ext_User_Edit_Email_Result($result['code'], $result['messages']);
        }
        $username = $userProfile['username'];
        if(!$username){
            $result['messages'][] = $this->_messager->createMessage(self::RECORD_NOT_FOUND);
            return new Ext_User_Edit_Email_Result($result['code'], $result['messages']);
        }
        
        $ucenter = new Ext_Ucenter();
        $uid = $ucenter->changeEmail($username, $email);
        if($uid < 0) {
            if($uid == -4) {
                $result['messages'][] = $this->_messager->createMessage(self::EMAIL_FORMAT_ERROR);
                
            } elseif($uid == -5) {
                $result['messages'][] = $this->_messager->createMessage(self::EMAIL_IS_DENIED);

            } elseif($uid == -6) {
                $result['messages'][] = $this->_messager->createMessage(self::EMAIL_ALREADY_EXISTS, array('email'=>$email));

            } elseif($uid == -7) {
                $result['messages'][] = $this->_messager->createMessage(self::NOTHING);
                
            } elseif($uid == -8) {
                $result['messages'][] = $this->_messager->createMessage(self::USER_IS_PROTECT);
            } else {
                $result['messages'][] = $this->_messager->createMessage(self::UNCATEGORIZED);
            }
            
            return new Ext_User_Edit_Email_Result($result['code'], $result['messages']);
        }
        
        $result['code'] = Ext_User_Edit_Email_Result::SUCCESS;
        return new Ext_User_Edit_Email_Result($result['code'], $result['messages']);
    }

}
